package com.loong.web.suppert.collection.param;

import java.util.HashMap;
import java.util.Map;

import com.loong.tool.lang.Strings;
import com.loong.web.suppert.collection.list.KvnList;

/**
 * 分页查询参数集
 *
 * @author 张成轩
 */
public class ParamMap extends HashMap<String, Object> {

	private static final long serialVersionUID = -8565302929529315772L;

	private PageParam page;

	/**
	 * @return 分页对象
	 */
	public PageParam getPage() {

		return page;
	}

	/**
	 * @param page 分页对象
	 */
	public void setPage(PageParam page) {

		this.page = page;
	}

	/**
	 * 添加模糊查询参数
	 * 
	 * @param key 键
	 * @param value 值
	 */
	public void putLike(String key, Object value) {

		put(key, "%" + value + "%");
	}

	/**
	 * 构建排序
	 * 
	 * @param orderType 排序类型映射
	 * @param orderField 排序字段映射
	 */
	public void buildOrder(Map<String, String> orderType, KvnList<String, String, String> orderField) {

		if (Strings.isNotNull(page.getOrder())) {
			StringBuilder order = new StringBuilder();
			for (String o : page.getOrder().split(",")) {
				String[] od = o.split("-");
				if (od.length == 2) {
					String field = orderField.getNByK(od[0]);
					String type = orderType.get(od[1]);
					if (field != null && type != null) {
						if (order.length() != 0)
							order.append(",");
						order.append(field).append(" ").append(type);
					}
				}
			}
			put("order", order.toString());
		} else
			remove("order");
	}
}
